package com.lpf;

import org.apache.http.HttpHost;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;

import java.io.IOException;

/**
 * 更新操作
 */
public class Main06 {

    public static void main(String[] args) throws IOException {

        RestHighLevelClient client = new RestHighLevelClient(
                RestClient.builder(
                        new HttpHost("localhost", 9200, "http"),
                        new HttpHost("localhost", 9200, "http")
                )
        );

        XContentBuilder builder = XContentFactory.jsonBuilder();

        builder.startObject();
        {
            builder.field("age", 30);
            builder.field("message", "update Test");
        }
        builder.endObject();

        // 创建 UpdateRequest 请求
        UpdateRequest request = new UpdateRequest("skywalking", "type", "1").doc(builder);

        // 发送请求
        UpdateResponse updateResponse = client.update(request);
        System.out.println(updateResponse);
    }
}